package com.examination.dao;

import com.examination.bean.Selection;
import com.examination.utils.DbUtil;
import com.examination.utils.PageInfo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @Auther: GongXl
 * @Date: 025 16:50
 * @Description:
 */
public class SelectionAdminDao {
    //查询
    public List<Selection> getSelectionList(Selection selection) {
        List<Selection> selectionList = null;
//        List<Map<String,Object>> objMapList = null;
        //数据库查询得到的
        Connection conn = DbUtil.getConnInstance();
        //3.操作数据库，实现增删改查
        ResultSet rs = null;
        PreparedStatement stmt = null;
        try {
            String sql = "SELECT * FROM e_selection where type = ?";
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, selection.getType());
            rs = stmt.executeQuery();
            try {
                selectionList = DbUtil.getObjectList(Selection.class,rs);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return selectionList;
    }

    public PageInfo<?> getSelectionListByPage(PageInfo<?> pageInfo) {
        List<Selection> selectionList = null;
        Connection conn = DbUtil.getConnInstance();
        //数据库记录数查询
        ResultSet rs = null;
        PreparedStatement stmt = null;
        int countNum = 0;//记录数
        try {
            String sql = "SELECT count(1) as countNum FROM e_selection where type = ?";
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, "single_choice");
            rs = stmt.executeQuery();
            try {
                while (rs.next()) {
                    countNum = rs.getInt("countNum");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        pageInfo.setCountNum(countNum);
        //分页查询
        try {
            String sql = "SELECT * FROM e_selection where type = ? limit ?,?";
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, "single_choice");
            stmt.setInt(2, pageInfo.getStart());
            stmt.setInt(3, pageInfo.getPageSize());
            rs = stmt.executeQuery();
            try {
                selectionList = DbUtil.getObjectList(Selection.class,rs);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        pageInfo.setObjList(selectionList);
        return pageInfo;
    }

    public String addSelection(Selection selection) {
        Connection conn = DbUtil.getConnInstance();
        PreparedStatement stmt = null;
        int row = 0;
        try {
            String sql = "insert into e_selection(type,content,choice_info,answer_info,answer,score) VALUES(?,?,?,?,?,?)";
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, selection.getType());
            stmt.setString(2, selection.getContent());
            stmt.setString(3, selection.getChoiceInfo());
            stmt.setString(4, selection.getAnswerInfo());
            stmt.setString(5, selection.getAnswer());
            stmt.setFloat(6, selection.getScore());
            row = stmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return row+"";
    }
    /**
     * @Author GongXl
     * @Description 通过id集合查询数据集
     * @Date 029 16:30
     * @Param [selection]
     * @return java.util.List<com.examination.bean.Selection>
     **/
    public List<Selection> getSelectionListByIds(Selection selection) {
        List<Selection> selectionList = null;
        Connection conn = DbUtil.getConnInstance();
        ResultSet rs = null;
        PreparedStatement stmt = null;
        try {
            String sql = "SELECT es.* from e_selection es\n" +
                    "where es.id in (?)";
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, (String)selection.getArgs().get("selectionIds"));
            rs = stmt.executeQuery();
            try {
                selectionList = DbUtil.getObjectList(Selection.class,rs);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return selectionList;
    }

}